/**
 * Copyright 2014-2015 www.fsfkj.com
 * All rights reserved.
 * 
 * @project
 * @author jipengfei
 * @version 1.0
 * @date 2015-11-21
 */
package com.eyaomall.wxmall.common.helper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.lycheeframework.core.common.helper.GlobEnv;
import com.lycheeframework.core.common.util.JSONUtil;

public class SchedulerHelper{
	
	public static final String MAIN_URL = GlobEnv.get("scheduler.url");
	
	private static HttpClient client;
	
	//新增定时任务接口
	private static final String ADD_JOB_URL = MAIN_URL + "job/add";
	
	//修改定时任务接口
	private static final String UPDATE_JOB_URL = MAIN_URL + "job/update";
	
	//暂停定时任务接口
	private static final String PAUSE_JOB_URL = MAIN_URL + "job/pause";
	
	//恢复定时任务接口
	private static final String RESUME_JOB_URL = MAIN_URL + "job/resume";
	
	//删除定时任务接口
	private static final String DEL_JOB_URL = MAIN_URL + "job/del";
	
	//校验定时任务是否存在接口
	private static final String CHECK_EXISTS_JOB_URL = MAIN_URL + "job/check_exists";
	
	public static HttpClient getClient(){
		if(client == null){
			client = HttpClientBuilder.create().build();
		}
		
		return client;
	}
	
	/**
	 * 新增任务
	 * @param url 回调地址
	 * @param paramsJson 用户自定义参数
	 * @param cronExpression 定时任务表达式
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static boolean addJobs(String url,String paramsJson,String cronExpression){
		HttpPost method = new HttpPost(ADD_JOB_URL);
		
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		NameValuePair urlParams = new BasicNameValuePair("callbackURL", url);
		NameValuePair jsonParams = new BasicNameValuePair("executionParam", paramsJson);
		NameValuePair cronParams = new BasicNameValuePair("cronExpression", cronExpression);
		params.add(urlParams);
		params.add(jsonParams);
		params.add(cronParams);
		
		try {
			HttpEntity  entity = new UrlEncodedFormEntity(params);
			method.setEntity(entity);
			
			HttpResponse response = getClient().execute(method);
			
			if(response.getStatusLine().getStatusCode() == 200){
				String json = EntityUtils.toString(response.getEntity(), "UTF-8");
				
				HashMap<String,Object> data = (HashMap<String, Object>) JSONUtil.toMap(json);
				if(0 == (int)data.get("ret")){
					
					return (boolean) data.get("data");
				}else{
					return false;
				}
			}
		} catch (IOException e) {
			//..
		}
		
		return false;
	}
	
	/**
	 * 修改任务
	 * @param url 回调地址
	 * @param paramsJson 用户自定义参数
	 * @param cronExpression 定时任务表达式
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static boolean updateJobs(String url,String paramsJson,String cronExpression){
		HttpPost method = new HttpPost(UPDATE_JOB_URL);
		
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		NameValuePair urlParams = new BasicNameValuePair("callbackURL", url);
		NameValuePair jsonParams = new BasicNameValuePair("executionParam", paramsJson);
		NameValuePair cronParams = new BasicNameValuePair("cronExpression", cronExpression);
		params.add(urlParams);
		params.add(jsonParams);
		params.add(cronParams);
		
		try {
			HttpEntity  entity = new UrlEncodedFormEntity(params);
			method.setEntity(entity);
			
			HttpResponse response = getClient().execute(method);
			
			if(response.getStatusLine().getStatusCode() == 200){
				String json = EntityUtils.toString(response.getEntity(), "UTF-8");
				
				HashMap<String,Object> data = (HashMap<String, Object>) JSONUtil.toMap(json);
				if(0 == (int)data.get("ret")){
					
					return (boolean) data.get("data");
				}else{
					return false;
				}
			}
		} catch (IOException e) {
			//..
		}
		
		return false;
	}
	
	/**
	 * 暂停任务
	 * @param url 回调地址
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static boolean pauseJobs(String url){
		HttpPost method = new HttpPost(PAUSE_JOB_URL);
		
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		NameValuePair urlParams = new BasicNameValuePair("callbackURL", url);
		params.add(urlParams);
		
		try {
			HttpEntity  entity = new UrlEncodedFormEntity(params);
			method.setEntity(entity);
			
			HttpResponse response = getClient().execute(method);
			
			if(response.getStatusLine().getStatusCode() == 200){
				String json = EntityUtils.toString(response.getEntity(), "UTF-8");
				
				HashMap<String,Object> data = (HashMap<String, Object>) JSONUtil.toMap(json);
				if(0 == (int)data.get("ret")){
					
					return (boolean) data.get("data");
				}else{
					return false;
				}
			}
		} catch (IOException e) {
			//..
		}
		
		return false;
	}
	
	/**
	 * 恢复任务
	 * @param url 回调地址
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static boolean resumeJobs(String url){
		HttpPost method = new HttpPost(RESUME_JOB_URL);
		
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		NameValuePair urlParams = new BasicNameValuePair("callbackURL", url);
		params.add(urlParams);
		
		try {
			HttpEntity  entity = new UrlEncodedFormEntity(params);
			method.setEntity(entity);
			
			HttpResponse response = getClient().execute(method);
			
			if(response.getStatusLine().getStatusCode() == 200){
				String json = EntityUtils.toString(response.getEntity(), "UTF-8");
				
				HashMap<String,Object> data = (HashMap<String, Object>) JSONUtil.toMap(json);
				if(0 == (int)data.get("ret")){
					
					return (boolean) data.get("data");
				}else{
					return false;
				}
			}
		} catch (IOException e) {
			//..
		}
		
		return false;
	}
	
	/**
	 * 删除任务
	 * @param url 回调地址
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static boolean delJobs(String url){
		HttpPost method = new HttpPost(DEL_JOB_URL);
		
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		NameValuePair urlParams = new BasicNameValuePair("callbackURL", url);
		params.add(urlParams);
		
		try {
			HttpEntity  entity = new UrlEncodedFormEntity(params);
			method.setEntity(entity);
			
			HttpResponse response = getClient().execute(method);
			
			if(response.getStatusLine().getStatusCode() == 200){
				String json = EntityUtils.toString(response.getEntity(), "UTF-8");
				
				HashMap<String,Object> data = (HashMap<String, Object>) JSONUtil.toMap(json);
				if(0 == (int)data.get("ret")){
					
					return (boolean) data.get("data");
				}else{
					return false;
				}
			}
		} catch (IOException e) {
			//..
		}
		
		return false;
	}
	
	/**
	 * 删除任务
	 * @param url 回调地址
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static boolean checkExistsJobs(String url){
		HttpPost method = new HttpPost(CHECK_EXISTS_JOB_URL);
		
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		NameValuePair urlParams = new BasicNameValuePair("callbackURL", url);
		params.add(urlParams);
		
		try {
			HttpEntity  entity = new UrlEncodedFormEntity(params);
			method.setEntity(entity);
			
			HttpResponse response = getClient().execute(method);
			
			if(response.getStatusLine().getStatusCode() == 200){
				String json = EntityUtils.toString(response.getEntity(), "UTF-8");
				
				HashMap<String,Object> data = (HashMap<String, Object>) JSONUtil.toMap(json);
				if(0 == (int)data.get("ret")){
					
					return (boolean) data.get("data");
				}else{
					return false;
				}
			}
		} catch (IOException e) {
			//..
		}
		
		return false;
	}
	
	/**
	 * 获取定时任务服务地址
	 * @return
	 */
	public static String getMainUrl(){
		
		return GlobEnv.get("scheduler.url");
	}
}